package cheme.ProcessEditor.Parts.Equations;

import java.util.List;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;

import cheme.ProcessEditor.ChemePresentation;
import cheme.ProcessEditor.LeafFigureNavigator;
import cheme.ProcessEditor.Figures.DecoratableFigure;
import cheme.ProcessEditor.Figures.EquationFig;
import cheme.ProcessEditor.Figures.Util.BulletedFig;
import cheme.ProcessEditor.Figures.Util.CircularBulletFig;
import cheme.ProcessEditor.Table.AbstractTableRowPart;
import cheme.ProcessEditor.Util.SelectionHelper;
import cheme.ProcessModel.Equations.Equation;

public class PartEquation extends AbstractTableRowPart
{
	@Override
	protected DecoratableFigure<BulletedFig<EquationFig>> createFigure()
	{
//		BulletedFig<EquationFig> figure = new BulletedFig<EquationFig>(new EquationFig(), new CircularBulletFig());
//
//		figure.setToolTip(new Label("This is single equation relating unknown values to known values."));
//		
//		return new DecoratableFigure<BulletedFig<EquationFig>>(figure);
		
		BulletedFig<EquationFig> figure = new BulletedFig<EquationFig>(new EquationFig(), new CircularBulletFig());
		
		figure.setToolTip(new Label("This is single equation relating unknown values to known values."));
		
		return new DecoratableFigure<BulletedFig<EquationFig>>(figure);
		
	}
	
	@Override
	protected void createEditPolicies()
	{
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new PolicyEquation());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new PolicyEquationLayout());
	}
	
	public Equation getModel()
	{
		return (Equation)super.getModel();
	}
	
	public DecoratableFigure<BulletedFig<EquationFig>> getFigure()
	{
		return (DecoratableFigure<BulletedFig<EquationFig>>)super.getFigure();
	}
	
	@Override
	public EquationFig getContentPane()
	{
		return getFigure().getContentPane().getContentPane();
	}
	
	@Override
	protected List getModelChildren()
	{
		return getModel().getModelChildAndAnnotations();
	}
	
	@Override
	protected void addChildVisual(EditPart childEditPart, int index)
	{
		if(getModel().isInitial())
		{
			((GraphicalEditPart)childEditPart).getFigure().setForegroundColor(ChemePresentation.Fields.initialForeground);
			((GraphicalEditPart)childEditPart).getFigure().setBackgroundColor(ChemePresentation.Fields.initialBackground);
			((GraphicalEditPart)childEditPart).getFigure().setBorder(ChemePresentation.Fields.initialBorder);
		}
		
		super.addChildVisual(childEditPart, index);
	}

	@Override
	protected void addChild(EditPart child, int index)
	{
		/*
		 * If there was a specific field selected in the last component desc,
		 * select the same field in the new component desc. 
		 */

		super.addChild(child, index);
				
			GraphicalEditPart navigateFrom = SelectionHelper.getFirstLeaf((GraphicalEditPart)child);
			
			if(null != navigateFrom)
			{
				LeafFigureNavigator.navigateTo(navigateFrom);
			}
	}
	
	@Override
	protected void removeChild(EditPart child)
	{
			/*
			 * If the child being remove had primary focus, we navigate to the 
			 * nearest neighbor above. This is a nicety for table-like editing in the
			 * stream descriptor.
			 */
			
			boolean first = false;
			
			int childIndex = getChildren().indexOf(child);
			
			if(0 == childIndex)
			{
				first = true;
			}
			
			/*
			 * Since this equation aprt only holds field parts, there should be no need for this now... still... no harm.
			 */
			GraphicalEditPart navigateFrom = SelectionHelper.getFirstLeaf((GraphicalEditPart)child);
			
			if(null != navigateFrom)
			{
				if(first)
				{
					LeafFigureNavigator.navigateNearestSibling(navigateFrom, PositionConstants.EAST);
				}
				else
				{
					LeafFigureNavigator.navigateNearestSibling(navigateFrom, PositionConstants.WEST);
				}
			}
		
		super.removeChild(child);
	}
}
